/**
 * Created by terry on 21/11/29.
 */
var ImgSwiper = {
    CreateNew: function () {
        var obj={};
        obj.slides_per_view=2;
        obj.swiper;
        obj.container;
        obj.is_index_swiper=false;
        obj.noSlidesPerView=false;



        obj.init=function(){

            if ($(window).outerWidth()>1900){
                obj.slides_per_view=3;
            }
            if ($(window).outerWidth()>2100){
                obj.slides_per_view=4;
            }
            if ($(window).outerWidth()>2100){
                obj.slides_per_view=5;
            }

            if (!is_mobile||obj.noSlidesPerView) {
                if (obj.noSlidesPerView){
                    obj.slides_per_view="auto";
                }

                var slides_num=$(obj.container +' .swiper-slide').length;
                obj.swiper = new Swiper(obj.container, {
                    slidesPerView: obj.slides_per_view,
                    spaceBetween: 10,
                    centeredSlides: true,
                    loop: true,
                    loopAdditionalSlides: 2,
                    loopPreventsSlide: false,

                    navigation: {
                        nextEl: '',
                        prevEl: ''
                    },
                    disabledClass: "",
                    speed: 500,
                    on: {
                        loopFix:function(){
                          console.log("loopFix");
                        },
                        slideChangeTransitionStart: function () {
                            //swiper2.update();
                            obj.clearMoveSwiper();
                            //swiper2.update();

                        },
                        slideChangeTransitionEnd: function () {
                            setTimeout(function () {
                                obj.swiper.update()

                                obj.moveImgSwiper();
                            }, 0)
                            //console.log(obj.swiper);

                            if (obj.swiper){
                                changeSwiperNavActive($(obj.container),obj.swiper.realIndex,obj.swiper);
                                if(obj.is_index_swiper) {
                                    changeImgSwiperTxt(obj.swiper.realIndex);
                                }
                            }
                        },
                        slidePrevTransitionStart: function () {
                            //console.log("slidePrevTransitionStart");

                        }
                    }
                });
                obj.swiper.params.progress=0;

                swiperNavInit($(obj.container),slides_num,obj.swiper);

                if(obj.is_index_swiper) {
                    changeImgSwiperTxt(0);
                }

                obj.moveImgSwiper();


            }
            else{
                var interleaveOffset = 0.5; //视差比值

                var swiperOptions = {

                    loop: true,
                    speed: 1000,
                    grabCursor: true,
                    watchSlidesProgress: true,
                    mousewheelControl: true,
                    keyboardControl: true,
                    navigation: {
                        nextEl: "",
                        prevEl: ""
                    },
                    on: {
                        progress: function(swiper) {
                            for (var i = 0; i < swiper.slides.length; i++) {
                                var slideProgress = swiper.slides[i].progress;
                                var innerOffset = swiper.width * interleaveOffset;
                                var innerTranslate = slideProgress * innerOffset;
                                swiper.slides[i].querySelector(".img_container").style.transform =
                                    "translate3d(" + innerTranslate + "px, 0, 0)";
                            }
                        },
                        touchStart: function(swiper) {
                            for (var i = 0; i < swiper.slides.length; i++) {
                                swiper.slides[i].style.transition = "";
                            }
                        },
                        setTransition: function(swiper, speed) {
                            for (var i = 0; i < swiper.slides.length; i++) {
                                swiper.slides[i].style.transition = speed + "ms";
                                swiper.slides[i].querySelector(".img_container").style.transition =
                                    speed + "ms";
                            }
                        },
                        slideChangeTransitionStart: function () {
                            //swiper2.update();
                            obj.clearMoveSwiper();
                            //swiper2.update();

                        },
                        slideChangeTransitionEnd: function () {
                            setTimeout(function () {
                                obj.swiper.update()
                                obj.moveImgSwiper();
                            }, 10)

                            if (obj.swiper){
                                changeSwiperNavActive($(obj.container),obj.swiper.realIndex,obj.swiper);
                                if (obj.is_index_swiper) {
                                    changeImgSwiperTxt(obj.swiper.realIndex);
                                }
                            }
                        }
                    }
                };

                var slides_num=$(obj.container+" .swiper-slide").length;
                obj.swiper = new Swiper(obj.container, swiperOptions);
                obj.swiper.params.progress=0;
                swiperNavInit($(obj.container),slides_num,obj.swiper);

                if (obj.is_index_swiper) {
                    changeImgSwiperTxt(0);
                }

                obj.moveImgSwiper();



                //$(container).find(".img").removeClass("move_img_x");
            }

        }

        obj.move_si=0;
        obj.move_st=0;
        obj.moveImgSwiper=function(){
            clearInterval(obj.move_si);
            clearTimeout(obj.move_st);
            obj.move_si=setInterval(function(){
                //var tx=getMatrix($(".story .swiper-container .swiper-wrapper")[0]).x-1;

                //$(".story .swiper-container .swiper-wrapper").css("transform","translate3d("+tx+"px,0,0)");
                //num--;
                obj.swiper.translateTo(obj.swiper.translate-1,0,true,true);

                //$(".story .swiper-container .swiper-wrapper").bind("mousedown");
                //swiper2.updateSize()
                //swiper2.updateActiveIndex();
                //swiper2.updateSlidesClasses();
            },40)

            obj.move_st=setTimeout(function(){
                //swiper2.update()
                obj.swiper.slideNext(2000,true);
                clearInterval(obj.move_si);
            },3000)

        }

        obj.clearMoveSwiper=function(){
            clearInterval(obj.move_si);
            clearTimeout(obj.move_st);
        }

        return obj
    }

}